/*
 * Ingredient.java
 *
 * Created on 18 de julio de 2007, 17:55
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package model;

import java.text.DateFormat;
import java.util.Date;
import java.util.Vector;
import org.w3c.dom.Document;

/**
 *
 * @author mariano
 */
public class Ingredient extends xml.Xmlizable{
    private String title;
    private String description;
    private int revision;
    private Vector<Author> authors;
    private Vector<Revision> revisions;
    private Vector<String> tags; 
    private Vector<Step> steps;
    
    /** Creates a new instance of Ingredient */
    public Ingredient() {
        authors = new Vector<Author>();
        revisions = new Vector<Revision>();
        tags = new Vector<String>();
        steps = new Vector<Step>();
    }
    
    public Ingredient( String title, int revision, Vector<Author> authors, Vector<Revision> revisions, String description, Vector<String> tags, Vector<Step> steps ) {
        this.title = title;
        this.revision = revision;
        this.authors = authors;
        this.revisions = revisions;
        this.description = description;
        this.tags = tags;
        this.steps = steps;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getRevision() {
        return revision;
    }

    public void setRevision(int revision) {
        this.revision = revision;
    }

    public Vector<Author> getAuthors() {
        return authors;
    }

    public void setAuthors(Vector<Author> authors) {
        this.authors = authors;
    }

    public Vector<Revision> getRevisions() {
        return revisions;
    }

    public void setRevisions(Vector<Revision> revisions) {
        this.revisions = revisions;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Vector<String> getTags() {
        return tags;
    }

    public void setTags(Vector<String> tags) {
        this.tags = tags;
    }

    public Vector<Step> getSteps() {
        return steps;
    }

    public void setSteps(Vector<Step> steps) {
        this.steps = steps;
    }
    
    public xml.Coleccion toXmlColeccion()
    {
        xml.Coleccion col = new xml.Coleccion( "ingredient" );
        col.addNodo( "ingredient-title", this.title );
        col.addNodo( "ingredient-revision", "" + this.revision );
        col.addNodo( "ingredient-description", this.description );
        
        xml.Coleccion col1 = new xml.Coleccion( "ingredient-authors" );
        
        for( Author author:authors )
        {
            col1.addItem( author.toXmlColeccion() );
        }
        
        xml.Coleccion col2 = new xml.Coleccion( "ingredient-revisions" );
        
        for( Revision revision:revisions )
        {
            col2.addItem( revision.toXmlColeccion() );
        }
        
        xml.Coleccion col3 = new xml.Coleccion( "ingredient-tags" );
        
        for( String tag:tags )
        {
            col3.addNodo( "ingredient-tag", tag );
        }
        
        xml.Coleccion col4 = new xml.Coleccion( "ingredient-steps" );
        
        for( Step step:steps )
        {
            col4.addItem( step.toXmlColeccion() );
        }
        
        col.addItem( col1 );
        col.addItem( col2 );
        col.addItem( col3 );
        col.addItem( col4 );
        
        return col;
    }
    
    public static Ingredient fromXml( String str )
    {
        xml.Item col = xml.Item.fromXml( str );
        return parse( col );
    }
    
    public static Ingredient fromXml( Document doc )
    {
        xml.Item col = xml.Item.fromXml( doc.getFirstChild() );
        return parse( col );
    }
    
    private static Ingredient parse( xml.Item col )
    {
        if( !col.isColeccion() || col.getName() != "ingredient" )
            return null;
        
        Vector<Author> vAuthors = new Vector<Author>();
        Vector<Revision> vRevisions = new Vector<Revision>();
        Vector<String> vTags = new Vector<String>();
        Vector<Step> vSteps = new Vector<Step>();
        
        int revisionNumber = 1;
        
        xml.Coleccion c = (xml.Coleccion)col;
        
        xml.Nodo nTitle = c.getNodo( 0, "ingredient-title" );
        xml.Nodo nRevision = c.getNodo( 1, "ingredient-revision" );
        xml.Nodo nDescription = c.getNodo( 2, "ingredient-description" );
        
        xml.Coleccion cAuthors = c.getColeccion( 3, "ingredient-authors" );
        xml.Coleccion cRevisions = c.getColeccion( 4, "ingredient-revisions" );
        
        xml.Coleccion cTags;
        xml.Coleccion cSteps;
        
        if( cRevisions == null )
        {
            cTags = c.getColeccion( 4, "ingredient-tags" );
            cSteps = c.getColeccion( 5, "ingredient-steps" );
        }
        else
        {
            cTags = c.getColeccion( 5, "ingredient-tags" );
            cSteps = c.getColeccion( 6, "ingredient-steps" );
        }
        
        // permitimos un ingredient sin revisions pero todos los otros campos
        // son obligatorios
        if( nTitle == null || nRevision == null || nDescription == null || 
            cAuthors == null || cTags == null || cSteps == null )
            return null;
        
        for( int i = 0 ; i < cAuthors.size() ; i++ )
        {
            xml.Coleccion authCol = cAuthors.getColeccion( i, "author" );
            
            if( authCol == null )
                continue;
            
            Author author = Author.fromXml( authCol.toString() );
            vAuthors.add( author );
        }
        
        if( cRevisions != null )
        {
            for( int i = 0 ; i < cRevisions.size() ; i++ )
            {
                xml.Coleccion revCol = cRevisions.getColeccion( i, "revision" );

                if( revCol == null )
                    continue;

                Revision revision = Revision.fromXml( revCol.toString() );
                vRevisions.add( revision );
            }
        }
        
        for( int i = 0 ; i < cTags.size() ; i++ )
        {
            xml.Nodo nTag = cTags.getNodo( i, "ingredient-tag" );
            
            if( nTag == null )
                continue;
            
            vTags.add( nTag.getValue() );
        }
        
        for( int i = 0 ; i < cSteps.size() ; i++ )
        {
            xml.Coleccion stepCol = cSteps.getColeccion( i, "step" );
            
            if( stepCol == null )
                continue;
            
            Step step = Step.fromXml( stepCol.toString() );
            vSteps.add( step );
        }
        
        try
        {
            revisionNumber = Integer.parseInt( nRevision.getValue() );
        }
        catch( Exception ex ){}
        
        return new Ingredient(nTitle.getValue(),revisionNumber,vAuthors,vRevisions,nDescription.getValue(),vTags,vSteps);
    }
}
